//! testdriver for drive simulation
/*!
	\file main.cpp
	\author Andreas Lehner
	\date 22.11.2012

*/
#include <fstream>
#include <time.h>
#include <Windows.h>
#include "PKW.h"
#include "TachometerObserver.h"
#include "OdometerObserver.h"

using namespace std;

int GetRandVal( int Lo, int Hi) {
	int Range = Hi - Lo + 1;
	int Num = rand() / (RAND_MAX + 1) * Range;
	return Lo + Num;
}

//! Generates testdata
/*!
This function generates random revolution data.
\param filename The name of the file the data should be stored in.
\param amount Determines how many data should be created
\param [optional] rev_bottom The minimum revolution number.
\param [optional] rev_top the initial maximum revolution number.
*/
void GenerateTestData(string const& filename, size_t amount, size_t rev_bottom = 550, size_t rev_top = 580) {
	ofstream Out(filename);
	srand((unsigned int)time(0));
	int Var = 0;
	int Delta = 8;
	for (size_t i = 1; i <= amount; i++)
	{
		Out << GetRandVal(rev_bottom + Var, rev_top + Var) << endl;
		Var += Delta;
		if (i % 50 == 0)
		{
			Delta = -Delta;
		}
	}

	Out.close();
}

int main() {
	GenerateTestData("RevolutionData.txt", 500);

	PKW p1(600, new WheelSpinSensor("RevolutionData.txt"));
	//Observers for Tachometer and Odometer
	TachometerObserver *pTacho = new TachometerObserver(&p1);
	OdometerObserver * pOdo = new OdometerObserver(&p1);
	//Update every 500 milliseconds
	while (true) {
		p1.Process();
		Sleep(500);
	}

	delete pTacho; pTacho = 0;
	delete pOdo; pOdo = 0;

	return 0;
}